package org.steven.crm.app.api.processor;

import com.steven.validate.MobileValidator;
import com.steven.validate.StringValidator;
import com.steven.xweb.web.CheckList;
import org.steven.crm.app.api.processor.base.BaseProcessor;
import org.steven.crm.core.Constants;
import org.steven.crm.core.dto.user.BindRequest;
import org.steven.crm.core.dto.user.LoginRequest;
import org.steven.crm.core.service.authcode.AuthCodeService;
import org.steven.crm.core.service.user.UserService;
import org.steven.crm.core.service.user.WechatUserService;
import org.steven.crm.core.utils.JSONUtil;

/**
 * @Author wuzw
 * @Date 2023/6/24 12:32
 * @Version 1.0
 */
public class WeixinUserProcessor extends BaseProcessor {
    private static UserService service = new UserService();

    private static WechatUserService wechatUserService = new WechatUserService();

    private static AuthCodeService authCodeService = new AuthCodeService();

    public boolean loginActionCheck() throws Exception {
        CheckList checkList = getChecklist();
        checkList.addCheckItem(new StringValidator("jsCode", "微信jsCode",
                getStringData("jsCode"), true));

        return checkList.check();
    }

    public void loginAction() throws Exception {
        String code = getStringData("jsCode");

        LoginRequest loginRequest = LoginRequest.builder().build()
                .setCode(code).setClientId(Constants.CLIENT.WECHAT_APP);

        setObjectData(wechatUserService.login(loginRequest));
    }

    public boolean bindActionCheck() throws Exception {
        CheckList checkList = getChecklist();
        checkList.addCheckItem(new MobileValidator("mobile", "手机号",
                getStringData("mobile"), true));
        checkList.addCheckItem(new StringValidator("password", "登录密码",
                getStringData("password"), true));
        checkList.addCheckItem(new StringValidator("bindCode", "绑定验证码",
                getStringData("bindCode"), true));
        return checkList.check();
    }

    public void bindAction() throws Exception {
        BindRequest bindRequest = JSONUtil.toBean(getStringDatas(), BindRequest.class);
        wechatUserService.bindUser(bindRequest);
    }

}
